class Solution:
    def __init__(self):
        self.sum = 0

    def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        self.dfs(root)
        return root

    def dfs(self, root):
        if not root:
            return None
        self.dfs(root.right)
        x = root.val
        root.val += self.sum 
        self.sum += x
        self.dfs(root.left)

